package com.goodmr.passport.webapp.Serivce.impl;

import com.goodmr.passport.account.AccountTypeEnum;
import com.goodmr.passport.account.PlatformModel;
import com.goodmr.passport.account.PrimaryEnum;
import com.goodmr.passport.password.PasswordDao;
import com.goodmr.passport.webapp.Serivce.RegisterService;
import com.goodmr.passport.webapp.enums.ErrorEnum;
import com.goodmr.passport.webapp.exception.BadRequestException;
import com.goodmr.passport.webapp.model.request.SignupReq;
import com.goodmr.passport.webapp.model.response.SignupResp;
import lombok.NonNull;
import org.philcode.toolkit.core.util.RegexUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;

/**
 * 帐号注册服务
 *
 * @author <a href="mailto:bingdong13@msn.com">bigo</a>
 */
@Service
public class RegisterServiceImpl extends AccountServiceImpl implements RegisterService {

    @Autowired
    private PasswordDao passwordDao;

    /**
     * 帐号注册
     *
     * @param data 帐号信息
     * @return signup response
     */
    @Override
    @Transactional
    public SignupResp register(@NonNull SignupReq data) {

        validateAccount(data.getAccount());

        validatePassword(data.getPassword());

        validateCode(data.getAccount(), data.getCode());

        validateAccountExist(data.getAccount());

        // 创建帐号信息
        PlatformModel platform = generatePlatform(data);

        // 设置密码
        passwordDao.generate(platform.getAccountId(), data.getPassword());

        return doAfter(platform);
    }

    /**
     * 创建帐号平台信息
     *
     * @param data 帐号信息
     * @return 帐号平台信息
     */
    private PlatformModel generatePlatform(@NonNull SignupReq data) {

        PlatformModel platformModel = new PlatformModel();

        String account = data.getAccount();
        String appId;

        if (isMobile(account)) {

            platformModel.setAccountType(AccountTypeEnum.MOBILE);
            appId = account.substring(0, 3);

        } else {

            platformModel.setAccountType(AccountTypeEnum.EMAIL);
            appId = account.substring(account.indexOf("@") + 1, account.length());
        }

        platformModel.setOpenId(account);
        platformModel.setIsPrimary(PrimaryEnum.YES);
        platformModel.setAppId(appId);

        return accountDao.generate(platformModel);
    }

    /**
     * 判断帐号是否存在
     *
     * @param account 帐号
     */
    private void validateAccountExist(@NonNull String account) {

        if (accountDao.existsByAccount(account)) {

            List<Object> args = Collections.singletonList(account);

            if (isMobile(account)) {
                ErrorEnum.ACCOUNT_MOBILE_EXIST.setArgs(args);
                throw new BadRequestException(ErrorEnum.ACCOUNT_MOBILE_EXIST);
            } else {
                ErrorEnum.ACCOUNT_EMAIL_EXIST.setArgs(args);
                throw new BadRequestException(ErrorEnum.ACCOUNT_EMAIL_EXIST);
            }
        }
    }

    /**
     * 注册成功，其它逻辑处理
     *
     * @param platform 帐号信息
     * @return signup response
     */
    private SignupResp doAfter(@NonNull PlatformModel platform) {

        // 添加设备信息

        // 生成注册日志

        // 生成授权凭证

        return new SignupResp("access_token", "refresh_token");
    }

    /**
     * 判断是否是手机帐号
     *
     * @param account 帐号
     * @return 如果是手机帐号，返回true，否则为false
     */
    private boolean isMobile(String account) {

        return RegexUtils.isMobile(account);
    }
}
